﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CJJCH.Web.Manage
{
    public partial class OLTypeAdd : PowerBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                ReapterDataBinding();
            }
            LTP.Common.ButtonClass.ButtonOnClick(this.Page, btnAdd, "checkform()", false);

        }

        protected void InitControl()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"]))
            {

                CJJCH.Model.T_NET_MenuSecond model = new Model.T_NET_MenuSecond();
                model = new CJJCH.BLL.T_NET_MenuSecond().GetModel(Convert.ToInt32(Request.QueryString["secondid"]));
                if (model != null)
                {

                    lblSecondId.Text = LTP.Common.StringClass.EncodeString(model.ID.ToString());
                    OnlineTop1.SecondId = Convert.ToInt32(lblSecondId.Text);
                    lblSecondName.Text = LTP.Common.StringClass.EncodeString(model.MenuName);
                    OnlineTop1.SecondName = LTP.Common.StringClass.EncodeString(lblSecondName.Text);
                    lblFirstID.Text = LTP.Common.StringClass.EncodeString(model.FirstID.ToString());
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }
              
            }
            if (Request.QueryString["Guid"] != null && LTP.Common.CheckInput.CheckGUID(Request.QueryString["Guid"].Trim()))
            {
                lblGuid.Text = LTP.Common.StringClass.EncodeUrl(Request.QueryString["Guid"].ToString());
            }
            else
            {
                lblGuid.Text = Convert.ToString(Guid.NewGuid());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["keyWord"]))
            {
                txtOnline.Text = Request.QueryString["keyWord"].ToString();
            }
        }

        protected void lbtnAdd_Click(object sender, EventArgs e)
        {

            Model.T_NET_TempOLItem model = new Model.T_NET_TempOLItem();
            string error = CheckInput();
            if (error == "")
            {
                model.ItemName = txtItem.Text;
                model.ManagerID = SessionManagerID;
                model.BuildDate = DateTime.Now;
                model.TypeID = new Guid(lblGuid.Text);
                model.ID = Guid.NewGuid();
                error = (new BLL.T_NET_TempOLItem()).AddOlItem(model);
                if (error == "")
                {
                    LTP.Common.WindowClass.WindowGoHref("OLTypeAdd.aspx?secondid="
                           + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                           + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text.Trim())
                           + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text.Trim())
                           + "&keyWord=" + LTP.Common.StringClass.EncodeUrl(txtOnline.Text)
                           + "&Guid=" + LTP.Common.StringClass.EncodeUrl(lblGuid.Text.Trim())
                          );
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }

            }
            else
            {
                LTP.Common.WindowClass.WindowBack(error);
            }
        }
        protected void lbtbDel_Click(object sender, EventArgs e)
        {
            if (LTP.Common.CheckInput.CheckGUID(((LinkButton)sender).CommandArgument))
            {

                string error = "";
                error = (new CJJCH.BLL.T_NET_TempOLItem()).DelTemp((new Guid(((LinkButton)sender).CommandArgument)));
                if (string.IsNullOrEmpty(error.Trim()))
                {
                    LTP.Common.WindowClass.WindowGoHref("OLTypeAdd.aspx?secondid="
                         + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                         + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text.Trim())
                         + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text.Trim())
                         + " &keyWord=" + LTP.Common.StringClass.EncodeUrl(txtOnline.Text)
                         + "&Guid=" + LTP.Common.StringClass.EncodeUrl(lblGuid.Text));
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }

        }
        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            if (LTP.Common.CheckInput.CheckGUID(((LinkButton)sender).CommandArgument))
            {
                StringBuilder error = new StringBuilder();
                LinkButton linb = (LinkButton)sender;
                int Index = ((RepeaterItem)(linb.NamingContainer)).ItemIndex;
                TextBox txtTypeName = (TextBox)rptType.Items[Index].FindControl("txtTypeName");
                if (txtTypeName.Text.Trim() == "")
                {
                    error.Append("请输入条目名称！！\\n");
                }


                if (!string.IsNullOrEmpty(error.ToString()))
                {
                    LTP.Common.MessageBox.Show(this, error.ToString());
                    return;
                }
                CJJCH.Model.T_NET_TempOLItem model = new Model.T_NET_TempOLItem();
                model.ItemName = LTP.Common.StringClass.EncodeString(txtTypeName.Text.Trim());
                model.TypeID = new Guid(lblGuid.Text);
                model.ManagerID = SessionManagerID;
                model.BuildDate = DateTime.Now;
                model.ID = new Guid(((LinkButton)sender).CommandArgument);
                string res = (new BLL.T_NET_TempOLItem()).EditTemp(model);
                LTP.Common.MessageBox.ShowAndRedirect(this, res, "OLTypeAdd.aspx?secondid="
                         + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                         + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text.Trim())
                         + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text.Trim())
                         + "&Guid=" + LTP.Common.StringClass.EncodeUrl(lblGuid.Text));
            }
        }
        private void ReapterDataBinding()
        {

            IList<CJJCH.Model.T_NET_TempOLItem> list = new List<CJJCH.Model.T_NET_TempOLItem>();
            if (lblGuid.Text != "")
            {
                list = (new CJJCH.BLL.T_NET_TempOLItem()).GetTempOLItem(new Guid(lblGuid.Text));
                if (list.Count!=0)
                {
                    divOitem.Style.Add("display", "block");
                    this.rptType.DataSource = list;
                    this.rptType.DataBind();
                }
              
            }

        }
        protected void rptType_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((TextBox)e.Item.FindControl("txtTypeName")).Text = ((CJJCH.Model.T_NET_TempOLItem)e.Item.DataItem).ItemName;
                ((LinkButton)e.Item.FindControl("lbtnEdit")).CommandArgument = ((CJJCH.Model.T_NET_TempOLItem)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).CommandArgument = ((CJJCH.Model.T_NET_TempOLItem)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).Attributes.Add("onclick", "javascript:if(confirm('确定要删除该数据吗？')){return true;}else{return false;}");

            }
        }
        

        protected string CheckInput()
        {
            StringBuilder error = new StringBuilder();
            if (txtItem.Text.Trim() == "")
            {
                error.Append("请输入条目名称！！\\n");
            }
            return error.ToString();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            if (txtOnline.Text.Trim() != "")
            {
                Model.T_NET_OLType type = new Model.T_NET_OLType();
                type.ID = new Guid(lblGuid.Text);
                type.TypeName = LTP.Common.StringClass.EncodeStr(txtOnline.Text.Trim());
                type.ManagerID = SessionManagerID;
                type.BuildDate = DateTime.Now;
                IList<CJJCH.Model.T_NET_TempOLItem> list = new List<CJJCH.Model.T_NET_TempOLItem>();
                list = (new CJJCH.BLL.T_NET_TempOLItem()).GetTempOLItem(new Guid(lblGuid.Text));
                string error = (new BLL.T_NET_OLType()).AddType(type, list);
                if (error == "")
                {
                    LTP.Common.WindowClass.WindowGoHref("添加成功", "OLTypeAdd.aspx?secondid="
                         + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                         + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text.Trim())
                         + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text.Trim())
                     );
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }

            }
            else
            {
                LTP.Common.WindowClass.WindowBack("主题名称不能为空！！");
            }
        }



    }
}